package com.serkol.ams.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.serkol.ams.entity.LoginLog;
import com.serkol.ams.entity.UsersAuthentication;
import com.serkol.ams.mapper.UsersAuthenticationMapper;
import com.serkol.ams.service.UsersAuthenticationService;
import com.serkol.constant.AuthConstant;
import org.springframework.stereotype.Service;

/**
 * @author pingan
 * @description 针对表【users_authentication】的数据库操作Service实现
 * @createDate 2022-09-17 16:31:38
 */
@Service
public class UsersAuthenticationServiceImpl extends ServiceImpl<UsersAuthenticationMapper, UsersAuthentication>
        implements UsersAuthenticationService {

    @Override
    public UsersAuthentication getInfo() {
        LoginLog loginLog = AuthConstant.getLoginLog();
        LambdaQueryWrapper<UsersAuthentication> queryWrapper = new LambdaQueryWrapper<UsersAuthentication>();
        queryWrapper.eq(UsersAuthentication::getUserId, loginLog.getUserId());
        queryWrapper.last("LIMIT 1");
        if (this.baseMapper.exists(queryWrapper)) {
            return this.getOne(queryWrapper);
        } else {
            return null;
        }

    }

    @Override
    public UsersAuthentication getFindByUserId(Long userId) {
        LambdaQueryWrapper<UsersAuthentication> queryWrapper = new LambdaQueryWrapper<UsersAuthentication>();
        queryWrapper.eq(UsersAuthentication::getUserId,userId);
        queryWrapper.last("LIMIT 1");
        if (this.baseMapper.exists(queryWrapper)) {
            UsersAuthentication one = this.getOne(queryWrapper);
            return one;
        } else {
            return null;
        }
    }

    @Override
    public boolean isIdCardValid(String idcard) {
        QueryWrapper<UsersAuthentication> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda().eq(UsersAuthentication::getIdcard, idcard);
        boolean exists = this.baseMapper.exists(queryWrapper);
        return exists;
    }

    @Override
    public boolean saveOrUpdate(UsersAuthentication usersAuthentication, QueryWrapper<UsersAuthentication> usersAuthenticationQueryWrapper) {
        if (usersAuthenticationQueryWrapper != null) {
            return this.update(usersAuthentication, usersAuthenticationQueryWrapper);
        }
        if (usersAuthentication != null) {
            return this.saveOrUpdate(usersAuthentication);
        }
        return false;
    }


}




